﻿Ext.BLANK_IMAGE_URL = 'Content/scripts/extjs/resources/images/default/s.gif';
Ext.SSL_SECURE_URL = 'Content/scripts/extjs/resources/images/default/s.gif';

function cardOnline() {
    Ext.Msg.show({
        title: '在线打卡',
        msg: '提示:您确定在线打卡吗?',
        buttons: Ext.Msg.YESNO,
        fn: function (btn) {
            if (btn == 'yes') {
                Ext.Ajax.request({
                    url: '/Card.mvc/CardOnline',
                    method: 'POST',
                    success: function (response, options) {
                        var responseMessage = Ext.util.JSON.decode(response.responseText);
                        if (responseMessage.Result) {
                            Ext.ux.Toast.msg('打卡成功', "您的打卡时间是:" + responseMessage.Message);
                        } else {
                            Ext.MessageBox.show({
                                title: "打卡失败", msg: responseMessage.Message
                            });
                        }
                    },
                    failure: function (response, options) {
                        var responseMessage = Ext.util.JSON.decode(response.responseText);
                        Ext.MessageBox.show({
                            title: "打卡失败", msg: responseMessage.Message
                        });
                    }
                });
            }
        }
    });
}
function loginOut() {
    Ext.Msg.show({
        title: '退出系统',
        msg: '提示:退出系统前请注意保存数据,确定要退出系统吗?',
        buttons: Ext.Msg.YESNO,
        fn: function (btn) {
            if (btn == 'yes') {
                Ext.Ajax.request({
                    url: '/EmpProfile.mvc/Logout',
                    method: 'POST',
                    success: function () {
                        Cookies.clear("empId");
                        Cookies.clear("empName");
                        Cookies.clear("jobNo");
                        Cookies.clear("empBasicInfo");
                        window.location = "Login.aspx";
                    }
                });
            }
        }
    });
}

Ext.onReady(function () {
    Ext.QuickTips.init();
    Ext.lib.Ajax.defaultPostHeader += '; charset=utf-8';

    // 左侧主菜单
    var mainMenu = new Swfu.BackMgr.MainMenuTree({
        collapsible: true,
        split: true,
        margins: '3 0 3 3',
        iconCls: 'icon-menu',
        cmargins: '3 3 3 3'
    });

    var Templatetab = Ext.extend(Ext.Panel, {
        closable: true,
        initComponent: function () {
            Templatetab.superclass.initComponent.call(this);
        }
    });

    var maintab = function () {
        maintab.superclass.constructor.call(this, {
            title: 'Tab',
            id: 'tabViewer',
            enableTabScroll: true, //当tab过多是否滚动
            activeTab: 0,
            autoScroll: true, //是否允许滚动条
            tabPosition: 'top',
            plugins: new Ext.ux.TabCloseMenu(),
            layoutOnTabChange: true,
            items: [{
                id: 'welcome',
                title: ' 桌 面',
                xtype: 'Welcome',
                iconCls: 'icon-home'
            }]
        });
    };
    Ext.extend(maintab, Ext.TabPanel, {
        loadClass: function (node, name) {
            var title = node.attributes.text;
            var id = node.attributes.id;
            var tab = Ext.getCmp(id);
            if (tab) {
                this.setActiveTab(tab);
            } else {
                if (id == 'FileMgr') {//我的文档
                    var p = this.add(new Ext.Panel({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true,
                        html: '<iframe src="PersonalMain.aspx" frameborder="0" width=100% height=100%></iframe>'
                    })
                        );
                } else if (id == 'AuthFileMgr') {//授权文档
                    var p = this.add(new Ext.Panel({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true,
                        html: '<iframe src="AuthedMain.aspx" frameborder="0" width=100% height=100%></iframe>'
                    })
                        );
                } else if (id == 'ShareMgr') {//共享文档
                    var p = this.add(new Ext.Panel({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true,
                        html: '<iframe src="ShareMain.aspx" frameborder="0" width=100% height=100%></iframe>'
                    })
                        );
                } else if (id == 'NoticeMgr') {//通知公告
                    var p = this.add(new Ext.Panel({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true,
                        html: '<iframe src="NoticeList.aspx" frameborder="0" width=100% height=100%></iframe>'
                    })
                        );
                } else if (id == 'DeptMgr') {//部门管理
                    var p = this.add(new Ext.Panel({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true,
                        html: '<iframe src="DeptList.aspx" frameborder="0" width=100% height=100%></iframe>'
                    })
                        );
                } else if (id == 'RolesMgr') {//角色(非职位)管理
                    var p = this.add(new Swfu.BackMgr.RolesMgr({
                        id: 'roleMgr',
                        title: title,
                        iconCls: 'icon-' + 'roleMgr',
                        closable: true
                    }));
                } else if (id == 'EmpProfileMgr') {//员工档案管理
                    var p = this.add(new Swfu.BackMgr.EmpProfileMgr({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true
                    }));
                } else if (id == 'AuditionMgr') {//面试维护
                    var p = this.add(new Ext.Panel({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true,
                        html: '<iframe src="AuditionList.aspx" frameborder="0" width=100% height=100%></iframe>'
                    })
                        );
                } else if (id == 'TrainNeedsMgr') {//培训需求
                    var p = this.add(new Ext.Panel({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true,
                        html: '<iframe src="TrainNeedsList.aspx" frameborder="0" width=100% height=100%></iframe>'
                    })
                        );
                } else if (id == 'TrainSchemeMgr') {//培训计划
                    var p = this.add(new Ext.Panel({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true,
                        html: '<iframe src="TrainSchemeList.aspx" frameborder="0" width=100% height=100%></iframe>'
                    })
                        );
                } else if (id == 'InfoMarketMgr') {//直销信息维护
                    var p = this.add(new Swfu.BackMgr.InfoMarketMgr({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true
                    }));
                } else if (id == 'KnowledgeMgr') {//太阳神知识库
                    window.open('http://192.168.1.43:8088/apolloCms/');
                } else if (id == 'cmsProduct') {//产品部CMS
                    window.open('http://192.168.1.80:83/Home/LoginFromOa?jobNo=' + Cookies.get("jobNo"));
                } else if (id == 'VehicleMgr') {//车辆申请
                    var p = this.add(new Swfu.BackMgr.VehicleMgr({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true
                    }));
                } else if (id == 'EvectionMgr') {//出差申请
                    var p = this.add(new Swfu.BackMgr.EvectionMgr({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true
                    }));
                } else if (id == 'OvertimeMgr') {//加班申请
                    var p = this.add(new Swfu.BackMgr.OvertimeMgr({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true
                    }));
                } else if (id == 'VacationMgr') {//请假申请
                    var p = this.add(new Swfu.BackMgr.VacationMgr({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true
                    }));
                } else if (id == 'PerchaseMgr') {//采购申请
                    var p = this.add(new Swfu.BackMgr.PerchaseMgr({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true
                    }));
                } else if (id == 'MeetingRoomMgr') {//会议室申请
                    var p = this.add(new Swfu.BackMgr.MeetingRoomMgr({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true
                    }));
                } else if (id == 'ReceptionMgr') {//商务接待申请
                    var p = this.add(new Swfu.BackMgr.ReceptionMgr({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true
                    }));
                } else if (id == 'InviteMgr') {//招聘申请
                    var p = this.add(new Swfu.BackMgr.InviteMgr({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true
                    }));
                } else if (id == 'UserMgr') {//用户管理
                    var p = this.add(new Swfu.BackMgr.UserMgr({
                        id: 'userMgr',
                        title: title,
                        iconCls: 'icon-' + 'userMgr',
                        closable: true
                    }));
                } else if (id == 'WeeklyMgr') {//周报
                    var p = this.add(new Swfu.BackMgr.WeeklyMgr({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true
                    }));
                } else if (id == 'DailyMgr') {//工作日志
                    var p = this.add(new Ext.Panel({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true,
                        html: '<iframe src="DailyList.aspx" frameborder="0" width=100% height=100%></iframe>'
                    })
                        );
                } else if (id == 'CardMgr') {//考勤打卡
                    var p = this.add(new Ext.Panel({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true,
                        html: '<iframe src="CardLog.aspx" frameborder="0" width=100% height=100%></iframe>'
                    })
                        );
                } else if (id == 'DutyMgr') {//值班维护
                    var p = this.add(new Ext.Panel({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true,
                        html: '<iframe src="DutyList.aspx" frameborder="0" width=100% height=100%></iframe>'
                    })
                        );
                } else if (id == 'AnnualMgr') {//年假
                    var p = this.add(new Swfu.BackMgr.AnnualMgr({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true
                    })
                    );
                } else if (id == 'ContactMgr') {//公共通讯薄
                    var p = this.add(new Ext.Panel({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true,
                        html: '<iframe src="Contact.aspx" frameborder="0" width=100% height=100%></iframe>'
                    })
                        );
                } else if (id == 'DocNoMgr') {//文件号维护
                    var p = this.add(new Swfu.BackMgr.DocNoMgr({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true
                    }));
                } else if (id == 'CardSignMgr') {//考勤签核
                    var p = this.add(new Ext.Panel({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true,
                        html: '<iframe src="CardSignList.aspx" frameborder="0" width=100% height=100%></iframe>'
                    })
                        );
                } else if (id == 'AssetMgr') {//资产维护
                    var p = this.add(new Ext.Panel({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true,
                        html: '<iframe src="AssetList.aspx" frameborder="0" width=100% height=100%></iframe>'
                    })
                        );
                } else if (id == 'CardOnlineMgr') {//在线打卡
                    cardOnline();
                } else if (id == 'UserSelfMgr') {//个人信息
                    var url = 'EmpContactEdit.aspx';
                    var retVal = window.showModalDialog(url, window, 'dialogWidth=550px;dialogHeight=350px;status=no;help=no;');
                    if (retVal != undefined && retVal.indexOf('Succeed') >= 0) {
                        alert("个人信息修改成功~");
                    }
                } else if (id == 'ChgPwdWin') {//修改密码
                    var ChgPwdWin = new Swfu.BackMgr.ChgPwdWin();
                    ChgPwdWin.show();
                } else if (id == 'QuestionMgr') {//题库维护
                    var p = this.add(new Ext.Panel({
                        id: id,
                        title: title,
                        iconCls: 'icon-' + id,
                        closable: true,
                        html: '<iframe src="ExamTypeList.aspx" frameborder="0" width=100% height=100%></iframe>'
                    })
                        );
                } else if (id == 'SysExit') {//退出系统
                    loginOut();
                } else {
                    Ext.Msg.alert('提示:', '该功能正在实现中,敬请期待...');
                }
                this.setActiveTab(p);
            }
        }
    });
    setTimeout(function () {
        Ext.get('loading').fadeOut({
            remove: true
        });
        Ext.get('loading-mask').fadeOut({
            remove: true
        });
    }, 550);

    var status = new Ext.StatusBar({
        id: 'my_status_bar',
        items: ['-', 'Copyright&nbsp;&nbsp;&copy;2011&nbsp;&nbsp;太阳神营销总部', '-'],
        region: 'south'
    });

    ShengRi = new Ext.grid.GridPanel(
{
    id: 'shengRiGrid', border: false, store: SRStore = new Ext.data.Store(
    {
        url: '/EmpProfile.mvc/GetCelebrate', reader: new Ext.data.JsonReader(
        {
            id: 'shengRiReader', fields: ["Id", "Name", "BornDate"]
        })
    }), columns: [new Ext.grid.RowNumberer({ hidden: true }),
    {
        header: '寿星', width: 70, dataIndex: 'Name',
        renderer: function (value, cellmeta, currRow) {
            return "<img src='../Content/images/icons/cake.png' width='14px' height='14px'>" + value;
        }
    }, {
        header: '日期', width: 70, hidden: false, dataIndex: 'BornDate',
        renderer: function (value) {
            return eval("new " + value.substr(1, value.length - 2)).format('m.d')
        }
    }], sm: new Ext.grid.RowSelectionModel({
        singleSelect: true
    }), hideHeaders: true, loadMask: true
});
    SRStore.load();
    ShengRi.addListener("rowclick", rowClickSR);
    function rowClickSR(grid, rowIndex, e) {
        var selections = this.selModel.getSelections();
        var record = selections[0];
        window.open('BirthdayBless.aspx?empId=' + record.data.Id);
    }

    var viewport = new Ext.Viewport({
        layout: 'border',
        items: [
				{
				    collapsible: true,
				    region: 'north',
				    xtype: 'mainheader',
				    iconCls: 'icon-Sys',
				    border: true,
				    margins: '0',
				    cmargins: '0',
				    frame: true
				},
			    mainMenu,
			    {
			        region: 'center',
			        border: false,
			        items: [new maintab()],
			        layout: 'fit',
			        margins: '3 1 3 1',
			        cmargins: '3 1 3 1'
			    },
			    status,
			    {
			        region: 'east',
			        title: ' 生日祝福',
			        collapsible: true,
			        width: 170,
			        border: true,
			        margins: '3',
			        cmargins: '3',
			        iconCls: 'icon-help',
			        frame: true,
			        bodyStyle: "padding:0,0,3,0",
			        items: ShengRi, autoScroll: true
			    }
			]
    });
    var leftmenu = viewport.items.itemAt(1);
    var centermain = viewport.items.itemAt(2).items.itemAt(0);
    var name = "test";
    leftmenu.on('click', function (node, e) {
        if (node.isLeaf()) {
            if (node.id == 'menu-exit') {
                var path = window.location.pathname;
                path = path.substring(0, path.lastIndexOf('/') + 1);
                path += "userLogin.htm";
                window.location = path;
                return;
            }
            e.stopEvent();
            centermain.loadClass(node, name); // 取叶子节点属性
        }
    });
});